package com.javasm.gg;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.javasm.commons.base.BaseController;
import com.javasm.commons.entity.AxiosResult;
import com.javasm.commons.entity.TableDatas;
import com.javasm.commons.enums.E;
import com.javasm.commons.util.Gi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.javasm.commons.entity.AxiosResult.error;

@RestController
@RequestMapping("goods")
public class GoodsController extends BaseController {
    @Autowired
    private GoodsService gs;

    @PostMapping
    public AxiosResult save(@RequestBody Goods goods) {
        //查询编码是否已存在
        Goods g = gs.getOne(new QueryWrapper<Goods>().eq("gcode", goods.getGcode()));
        if (g != null) return error(E.EXISTS);

        Boolean flag = gs.save(goods);
        if (flag) return suc(flag);
        return error();
    }

    @GetMapping("select")//查询
    public TableDatas selectAll(Goods goods) {
        startPage();
        List<Goods> flag = gs.listGoodsAndGoodsType(goods);
        return new TableDatas(flag);
    }

    @GetMapping("goods2")//查询
    public TableDatas listGoods2(Goods goods) {
        startPage();
        List<Goods> flag = gs.listGoods2AndGoodsType(goods);
        return new TableDatas(flag);
    }

    @DeleteMapping("{id}")//删除
    public AxiosResult delById(@PathVariable("id") Integer id) {
        Boolean flag = gs.removeById(id);
        return isok(flag);
    }

    @PutMapping
    public AxiosResult update(@RequestBody Goods goods) {
        boolean b = gs.updateById(goods);
        if (b) return suc();
        return error();
    }

}











